//
//  AppDataConversionTool.m
//  YLLottery
//
//  Created by Apple on 2017/9/29.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "AppDataConversionTool.h"

@implementation AppDataConversionTool
+ (instancetype)shareInstance
{
    static AppDataConversionTool  *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    return _instance;
}

- (id)convertWithDictionary:(NSDictionary *)soureDictionary ToModel:(Class)targetClass
{
    if(soureDictionary == nil) return nil;
     NSError *error = nil;
    id model = [MTLJSONAdapter modelOfClass:targetClass fromJSONDictionary:soureDictionary error:&error];
    if(error != nil){
        NSLog(@"字典转模型失败，失败原因：%@",error);
    }
    return model;
}

- (NSDictionary *)convertWithModelToDictionary:(id<MTLJSONSerializing>)model
{
    if(model == nil) return nil;
    NSError *err = nil;
    NSDictionary *dic = [MTLJSONAdapter JSONDictionaryFromModel:model error:&err];
    if (err) {
        NSLog(@"模型转字典失败%@",err);
        return nil;
    }
    return dic;
}

- (NSString *)convertWithModelToJSONString:(id<MTLJSONSerializing>)model
{
    if(model == nil) return @"";
    NSDictionary *dict = [self convertWithModelToDictionary:model];
    NSError *error = nil;
    NSData  *jsonData =[NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
    if(error)
    {
        NSLog(@"模型转字典序列化失败%@",error);
        return nil;
    }
    else
        return [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
}

- (NSDictionary *)convertWithJSONStringToJSON:(NSString *)JSONString
{
    if(JSONString == nil) return nil;
    NSData *jsonData = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    if(error)
    {
        NSLog(@"字符串转字典反列化失败%@",error);
        return nil;
    }
    else
    {
        return dict;
    }
}

- (id)convertWithJSONString:(NSString *)JSONString ToModel:(Class)modelClass
{
    NSDictionary *dict = [self convertWithJSONStringToJSON:JSONString];
    return [self convertWithDictionary:dict ToModel:modelClass];
}

-(id)convertWithJSONArray:(NSArray *)JSONArray ToModelArray:(Class)modelClass
{
    if(JSONArray.count <= 0) return nil;
    NSMutableArray *objectArray = [NSMutableArray array];
    for (NSDictionary *dict in JSONArray) {
        id model = [self convertWithDictionary:dict ToModel:modelClass];
        [objectArray addObject:model];
    }
    return objectArray;
}


-(NSString *)convertWithModelArrayToJSONString:(NSArray<MTLJSONSerializing> *)modelArray{
    if (modelArray.count <= 0) {
        return @"";
    }
    
    NSMutableArray *JSONArray = [NSMutableArray array];
    for (id model in modelArray) {
        NSString *modelJSONStr =  [self convertWithModelToJSONString:model];
        NSLog(@"modelJSONStr:%@",modelJSONStr);
        [JSONArray addObject:modelJSONStr];
    }
    
    NSData *JSONData = [NSJSONSerialization dataWithJSONObject:JSONArray options:NSJSONWritingPrettyPrinted error:nil];
    
    NSString *JSONStr = [[NSString alloc] initWithData:JSONData encoding:NSUTF8StringEncoding];
    
    
    return JSONStr;
}



@end
